import { getToken,removeToken,setToken} from '@/utils/auth'
import { defineStore } from 'pinia'
import {ref} from 'vue'
import type { Profile } from '@/types/user'

export const useUserStore = defineStore('hm-user', () => {
  //优先从本地取
  const token=ref(getToken()||'')
  //个人信息
  const profile=ref<Profile>({} as Profile)
  const setProfile=(p:Profile)=>{
    profile.value=p
  }
  
  const setToken=(t:string)=>{
    token.value=t
    //本地存一份
    setToken(t)
  }

  //清除用户信息
  const clearUserInfo=()=>{
    //清除Token
    token.value=''
    removeToken()
  }
  return {token,setToken,clearUserInfo,profile,setProfile}
})
